<?php

if (! file_exists(realpath(dirname(__FILE__)) . '/../vendor/autoload.php'))
    exit("autoload文件不存在");

require realpath(dirname(__FILE__)) . '/../vendor/autoload.php';

use paySdk\functions\postar\lib\Webhook\Webhook;
use paySdk\Pay;

$response = ["rspCod" => "", "rspMsg" => "failure"];
try {
    // 获取post参数，将json字符串转成数组，在实例化webhook的时候传入
    $data = file_get_contents("php://input");
    $data = json_decode($data, true);
    $webhook = new Webhook($data);

    // 实例化Pay时将webhook对象传入，调用notify方法。生产环境下需要将mode设置为production
    $pay = new Pay(['webhook' => $webhook, "mode" => "testing"]);
    $pay->postar->notify();

    // 验签通过，执行相关业务逻辑
    // some coding...
    $response['rspMsg'] = "success";

} catch (Exception $e) {
    // 验签不通过
} finally {
    header("Content-type: application/json");
    exit(json_encode($response));
}


